package libra;

import java.lang.reflect.Field;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.aop.framework.AdvisedSupport;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
	@Autowired
	private LoadFile loadConfig;
	
	@RequestMapping("getData")
	public Object getDate(HttpServletRequest req, HttpServletResponse resp) {
		LoadFile load = (LoadFile)getTarget(loadConfig);
		return load;
	}
	
	// 获取被CGLIB代理的实际对象实例
	public static <T> T getTarget(T beanInstance) {
        if (!AopUtils.isAopProxy(beanInstance)) {
            return beanInstance;
        } else if (AopUtils.isCglibProxy(beanInstance)) {
            try {
                Field h = beanInstance.getClass().getDeclaredField("CGLIB$CALLBACK_0");
                h.setAccessible(true);
                Object dynamicAdvisedInterceptor = h.get(beanInstance);
 
                Field advised = dynamicAdvisedInterceptor.getClass().getDeclaredField("advised");
                advised.setAccessible(true);
                AdvisedSupport advisedSupport = (AdvisedSupport)advised.get(dynamicAdvisedInterceptor);
                T target = (T)advisedSupport.getTargetSource().getTarget();
                return target;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
